package com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.example.springboot.common.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
功能：提供接口返回数据
* */
@RestController

public class WebController {
  @Resource
  UserService UserService;
  private final UserService userService;

  public WebController(UserService userService) {
    this.userService = userService;
  }

  @GetMapping("/")
    public Result hello() {
        return Result.success("success");
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
    if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())) {
      return Result.error("数据输入不合法");
    }
    user  = userService.login(user);
    return Result.success(user);
    }

  /*新增用户信息*/
  @PostMapping("/add")
  public Result add(@RequestBody User user) {
    try {
      userService.insertUser(user);
    }catch (Exception e) {
      if(e instanceof DuplicateKeyException){
        return Result.error("插入数据错误");
      }else {
        return Result.error("系统错误 ");
      }
    }
    return Result.success();
  }
    @PostMapping("/post")
    public Result post(@RequestBody Obj obj) {
      System.out.println(obj.getName()==null?"null":obj.getName().isEmpty());
        return Result.success(obj);
    }

    @PutMapping("/put")
    public Result put(@RequestBody Obj obj) {
        return Result.success(obj);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(id);
    }
  /**
   * 多条件查询用户信息
   */
  @GetMapping("/selectByMore")
  public Result selectByMore(@RequestParam String username, @RequestParam String name) {
    List<User> userList = userService.selectByMore(username,name);
    return Result.success(userList);
  }
  @GetMapping("/selectAll")
  public Result selectAll() {
    List<com.example.springboot.demos.web.User> userList = userService.selectAll();
    return Result.success(userList);
  }
  @GetMapping("/selectById/{id}")
  public Result selectById(@PathVariable Integer id) {
    User user = userService.selectById(id);
    return Result.success(user);
  }
  @GetMapping("/selectByName/{name}")
  public Result selectByName(@PathVariable String name) {
    List<User> userList = userService.selectByName(name);
    return Result.success(userList);
  }
  @GetMapping("/selectByMo")
  public Result selectByMo(@RequestParam String username, @RequestParam String name) {
    List<User> userList = userService.selectByMo(username,name);
    return Result.success(userList);
  }
  @GetMapping("/selectByPage")
  public Result selectByPage(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize,
                             @RequestParam String username,
                             @RequestParam String name) {
    Page<com.example.springboot.demos.web.User> page = userService.selectByPage(pageNum, pageSize, username, name);
    return Result.success(page);
}

}
